package com.videoaggre.zcl.ui.fragment.enterdesk

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import com.videoaggre.zcl.api.ENTERDESK_SERVICE
import com.videoaggre.zcl.bean.EnterdeskClassify
import com.videoaggre.zcl.common.ENTERDESK_CLASSIFY
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.parse.impl.EnterdeskParse
import com.videoaggre.zcl.ui.fragment.launchUI
import com.videoaggre.zcl.ui.fragment.put
import com.videoaggre.zcl.ui.viewmodel.EnterdeskViewModel

class EnterdeskClassifyImageFragment : EnterdeskImageFragment() {
    companion object {
        fun createInstance(enterdeskClassify: EnterdeskClassify): EnterdeskClassifyImageFragment = EnterdeskClassifyImageFragment().put(ENTERDESK_CLASSIFY to enterdeskClassify)
    }

    private val mEnterdeskViewModel: EnterdeskViewModel by viewModels(::requireActivity)
    private val mEnterdeskClassify: EnterdeskClassify by InjectBundle()
    private var isSend = false
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        super.initView(rootView, savedInstanceState)
        mEnterdeskViewModel.brandDetilChangeLiveData.observe(this) {
            mEnterdeskClassify.path = it.path
            mRefreshHelper.onRefresh()
        }
    }

    override fun onReload() {
        launchUI({
            val html = ENTERDESK_SERVICE.classify(mEnterdeskClassify.path, if (mRefreshHelper.page == 1) "" else "${mRefreshHelper.page}.html")
            if (!isSend) {
                isSend = true
                mEnterdeskViewModel.brandDetilLiveData.value = html
            }
            EnterdeskParse.parseImages(html)
        }, mRefreshHelper.handApiObserver())
    }
}